/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nightonke.boommenusample;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;

import java.util.List;

public class PagerProvider extends PageSliderProvider {

    private List<DataItem> list;
    private AbilitySlice slice;

    public PagerProvider(List<DataItem> listIn, AbilitySlice sliceIn){
        this.list = listIn;
        this.slice = sliceIn;
    }

    /**
     * getCount
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * create PageInContainer
     * @param componentContainer
     * @param i
     * @return
     */
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        final DataItem data = list.get(i);

        //Component cpt = LayoutScatter.getInstance(slice).parse(data.mLayout, null, true);
        Component cpt = data.mLayout.getComponent();
        Text label = (Text)cpt.findComponentById(ResourceTable.Id_text_helloworld);
        label.setText(data.mText);
        componentContainer.addComponent(cpt);
        return cpt;
    }

    /**
     * destroy PageFrom Container
     * @param componentContainer
     * @param i
     * @param o
     */
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    /**
     * isPageMatchToObject
     * @param component
     * @param o
     * @return
     */
    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }

    //数据实体类
    public static class DataItem{
        private String mText;
        private BMBFragment mLayout;
        public DataItem(String txt, BMBFragment id) {
            mText = txt;
            mLayout = id;
        }
    }
}
